package cc.bak;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

import oracle.to.mysql.utils.SqlUtils;

public class CC21_ACC200_Insert {

	private static Logger log = Logger.getLogger(" ");
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			log.error("驱动加载异常：" + e.getMessage());
		}
	}
	private static final String CC21_SQL="select acc210,acc200,aac001 from cc21 where acc200='0' and DXP_CC21_ACC210='财务数据转换'";
	private static final String CC20_SQL="select acc200,aac001 from cc20 where aac001='{aac001}' and DXP_AC01_AAC001='财务数据转换'";
	
	public static void main(String[] args) throws SQLException, IOException {
		long time = System.currentTimeMillis();
		Connection connCC21 = DriverManager.getConnection(SqlUtils.getMySqlUrl());
		Statement cc21stmt = connCC21.createStatement();
		ResultSet rs = cc21stmt.executeQuery(CC21_SQL);
		Connection connMysql = DriverManager.getConnection(SqlUtils.getMySqlUrl());
		while (rs.next()) {
			String aac001 = rs.getString("aac001");
			String acc200 = rs.getString("acc200");
			String acc210 = rs.getString("acc210");
			if("0".equals(acc200)){
				String count = getCountByAAC001(connMysql, aac001);
				if (count == null) {
					log.error("mysql中不存在此数据，aac001=" + aac001 + ",acc200=" + acc200);
				} else {
					Statement stmt = connMysql.createStatement();
					stmt.execute("update cc21 set acc200='"+count+"' where acc200='0' and DXP_CC21_ACC210='财务数据转换' and acc210='"+acc210+"'");
					stmt.close();
				}
			}
		}
		cc21stmt.close();
		connCC21.close();
		
		connMysql.close();
		log.info("按主键，全部对比完成，耗时:" + (System.currentTimeMillis() - time) + "ms");
	}

	private static String getCountByAAC001(Connection conn, String aac001) throws SQLException {
		if(aac001 == null || aac001.length()==0){
			return null;
		}
		String sql = CC20_SQL.replace("{aac001}", aac001);
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery(sql);
		String count = null;
		if (rs.next()) {
			count = rs.getString(1);
		}
		rs.close();
		stmt.close();
		return count;
	}
	
}